#Partially from Odoo's dockerfile
#github.com/odoo/docker

FROM debian:stretch
MAINTAINER FlectraHQ <flectra@flectrahq.com>

# Generate locale C.UTF-8 for postgres and general locale data
ENV LANG C.UTF-8
ENV FLECTRA_VERSION=1.4
ENV FLECTRA_RELEASE=latest

# Install some deps, lessc and less-plugin-clean-css, and wkhtmltopdf
RUN set -x; \
        apt-get update \
        && apt-get install -y --no-install-recommends \
            ca-certificates \
            curl \
            gcc \
            libsasl2-dev \
            libldap2-dev \
            node-less \
            python3-dev \
            python3-pip \
            python3-setuptools \
            python3-renderpm \
            libssl1.0-dev \
            xz-utils \
        && curl -o wkhtmltox.tar.xz -SL https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz \
        && echo '3f923f425d345940089e44c1466f6408b9619562 wkhtmltox.tar.xz' | sha1sum -c - \
        && tar xvf wkhtmltox.tar.xz \
        && cp wkhtmltox/lib/* /usr/local/lib/ \
        && cp wkhtmltox/bin/* /usr/local/bin/ \
        && cp -r wkhtmltox/share/man/man1 /usr/local/share/man/

# Install Flectra Dependencies and WDB Debugger
RUN set -x; \
    pip3 install -r https://gitlab.com/flectra-hq/flectra/raw/v${FLECTRA_VERSION}.0/requirements.txt \
    && pip3 install --no-cache-dir wdb watchdog

ENV MQT_URI="https://gitlab.com/arkhan/developer-quality-tools/-/archive/master/developer-quality-tools-master.tar.gz"
RUN curl -sL "$MQT_URI" | tar -xz -C /opt/ \
    && ln -sf /opt/developer-quality-tools-*/gitlab-ci/clone_module_dependencies /usr/bin \
    && ln -sf /opt/developer-quality-tools-*/gitlab-ci/getaddons.py /usr/bin \
    && chmod +x /usr/bin/getaddons.py

# Copy entrypoint script and Flectra configuration file
COPY ./entrypoint.sh /
RUN adduser --system --quiet --shell=/bin/false --group flectra
#RUN chown flectra /etc/flectra/flectra.conf
RUN chmod +x /entrypoint.sh

# Mount /var/lib/flectra to allow restoring filestore and /mnt/extra-addons for users addons
RUN mkdir -p /mnt/repos \
        && chown -R flectra /mnt/repos
VOLUME ["/var/lib/flectra", "/mnt/repos"]

# Expose Flectra services
EXPOSE 7073 7072

# Set the default config file
ENV FLECTRA_RC /etc/flectra/flectra.conf

# Set default user when running the container
USER flectra

ENTRYPOINT ["/entrypoint.sh"]
CMD ["flectra"]
